#define _CRT_SECURE_NO_WARNINGS  1


class Solution {
    struct Greater
    {
        bool operator()(const pair<string, int>& kv1, const pair<string, int>& kv2)
        {
            return kv1.second > kv2.second;
        }
    }mm;
public:
    vector<string> topKFrequent(vector<string>& words, int k)
    {
        map<string, int> m;
        for (const auto& e : words)
        {
            m[e]++;
        }

        vector<pair<string, int>> vm(m.begin(), m.end());
        stable_sort(vm.begin(), vm.end(), mm);

        vector<string> ret;
        for (int i = 0; i < k; i++)
        {
            ret.push_back(vm[i].first);
        }
        return ret;
    }
};